package com.tgsoft.kdcloud.controller;


import com.google.gson.Gson;
import com.kingdee.bos.webapi.entity.IdentifyInfo;
import com.kingdee.bos.webapi.entity.RepoRet;
import com.kingdee.bos.webapi.sdk.K3CloudApi;


import com.tgsoft.kdcloud.common.SeqHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("car")
public class CarController {

    @Autowired
    private K3CloudApi k3CloudApi;

    @GetMapping("savesaleorder")
    public String carPurIn(){

        String FNumber = SeqHelper.genNumber("XSDD");
        String FName = "auxsdd_" + UUID.randomUUID();
        String saleid = "";

        String json = "{\n" +
                "        \"NeedUpDateFields\": [],\n" +
                "        \"NeedReturnFields\": [],\n" +
                "        \"IsDeleteEntry\": \"true\",\n" +
                "        \"SubSystemId\": \"\",\n" +
                "        \"IsVerifyBaseDataField\": \"False\",\n" +
                "        \"IsEntryBatchFill\": \"true\",\n" +
                "        \"ValidateFlag\": \"true\",\n" +
                "        \"NumberSearch\": \"true\",\n" +
                "        \"IsAutoAdjustField\": \"False\",\n" +
                "        \"InterationFlags\": \"\",\n" +
                "        \"IgnoreInterationFlag\": \"\",\n" +
                "        \"IsControlPrecision\": \"False\",\n" +
                "        \"Model\": {\n" +
                "    \"FBillTypeID\": {\n" +
                "      \"FNUMBER\": \"XSDD01_SYS\"\n" +
                "    },\n" +
                "    \"FDate\": \"2022-04-27 00:00:00\",\n" +
                "    \"FSaleOrgId\": {\n" +
                "      \"FNumber\": \"100\"\n" +
                "    },\n" +
                "    \"FCustId\": {\n" +
                "      \"FNumber\": \"SCMKH100001\"\n" +
                "    },\n" +
                "    \"FReceiveId\": {\n" +
                "      \"FNumber\": \"SCMKH100001\"\n" +
                "    },\n" +
                "    \"FSaleDeptId\": {\n" +
                "      \"FNumber\": \"SCMBM000001\"\n" +
                "    },\n" +
                "    \"FSalerId\": {\n" +
                "      \"FNumber\": \"SCMYG000001_SCMGW000001_1\"\n" +
                "    },\n" +
                "    \"FSettleId\": {\n" +
                "      \"FNumber\": \"SCMKH100001\"\n" +
                "    },\n" +
                "    \"FChargeId\": {\n" +
                "      \"FNumber\": \"SCMKH100001\"\n" +
                "    },\n" +
                "    \"FSaleOrderFinance\": {\n" +
                "      \"FSettleCurrId\": {\n" +
                "        \"FNumber\": \"PRE001\"\n" +
                "      },\n" +
                "      \"FIsPriceExcludeTax\": 'true',\n" +
                "      \"FIsIncludedTax\": 'true',\n" +
                "      \"FExchangeTypeId\": {\n" +
                "        \"FNumber\": \"HLTX01_SYS\"\n" +
                "      }\n" +
                "    },\n" +
                "    \"FSaleOrderEntry\": [\n" +
                "      {\n" +
                "        \"FRowType\": \"Standard\",\n" +
                "        \"FMaterialId\": {\n" +
                "          \"FNumber\": \"SCMWL100002\"\n" +
                "        },\n" +
                "        \"FUnitID\": {\n" +
                "          \"FNumber\": \"Pcs\"\n" +
                "        },\n" +
                "        \"FQty\": 10,\n" +
                "        \"FPriceUnitId\": {\n" +
                "          \"FNumber\": \"Pcs\"\n" +
                "        },\n" +
                "        \"FPrice\": 8.849558,\n" +
                "        \"FTaxPrice\": 10,\n" +
                "        \"FEntryTaxRate\": 13,\n" +
                "        \"FDeliveryDate\": \"2022-04-27 15:15:54\",\n" +
                "        \"FStockOrgId\": {\n" +
                "          \"FNumber\": \"100\"\n" +
                "        },\n" +
                "        \"FSettleOrgIds\": {\n" +
                "          \"FNumber\": \"100\"\n" +
                "        },\n" +
                "        \"FSupplyOrgId\": {\n" +
                "          \"FNumber\": \"100\"\n" +
                "        },\n" +
                "        \"FOwnerTypeId\": \"BD_OwnerOrg\",\n" +
                "        \"FOwnerId\": {\n" +
                "          \"FNumber\": \"100\"\n" +
                "        },\n" +
                "        \"FReserveType\": \"1\",\n" +
                "        \"FPriceBaseQty\": 10,\n" +
                "        \"FStockUnitID\": {\n" +
                "          \"FNumber\": \"Pcs\"\n" +
                "        },\n" +
                "        \"FStockQty\": 10,\n" +
                "        \"FStockBaseQty\": 10,\n" +
                "        \"FOUTLMTUNIT\": \"SAL\",\n" +
                "        \"FOutLmtUnitID\": {\n" +
                "          \"FNumber\": \"Pcs\"\n" +
                "        },\n" +
                "        \"FAllAmountExceptDisCount\": 100,\n" +
                "        \"FOrderEntryPlan\": [\n" +
                "          {\n" +
                "            \"FPlanDate\": \"2022-04-27 15:15:54\",\n" +
                "            \"FPlanQty\": 10\n" +
                "          }\n" +
                "        ]\n" +
                "      }\n" +
                "    ],\n" +
                "    \"FSaleOrderPlan\": [\n" +
                "      {\n" +
                "        \"FRecAdvanceRate\": 100,\n" +
                "        \"FRecAdvanceAmount\": 100\n" +
                "      }\n" +
                "    ],\n" +
                "    \"FBillNo\":"+"\""+FNumber+"\""+",\n" +
                "  }\n" +
                "    }";


        String result = null;
        try {
            result = k3CloudApi.save("SAL_SaleOrder", json);
            Gson gson = new Gson();
            RepoRet sRet = gson.fromJson(result, RepoRet.class);
            if (sRet.isSuccessfully()) {
                saleid = sRet.getResult().getId();
                System.out.printf("销售订单保存接口%s%n", gson.toJson(sRet.getResult()));
            }
        /*else {
            fail("销售订单保存接口: " + gson.toJson(sRet.getResult()));
        }*/
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return "sadf";
    }


    @GetMapping("restreq")
    public String carPurIn3() throws Exception {

        // 创建RestTemplate对象
        RestTemplate restTemplate = new RestTemplate();

        // 定义请求的URL
        String url = "http://222.82.221.18:8099/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.AuthService.LoginByAppSecret.common.kdsvc";
        //String url = "http://222.82.221.18:8099/k3cloud/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc";


        // 定义请求头
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type", "application/json");

        // 定义请求体内容
        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
        Object[] parameters = new Object[] { "6625e41c73f9d4", "kingdee", "276193_23drR6vOTOHYxX+GSd2P2y+E0iTb6CLH", "f57eb7f582264d7393be66d33d7dc8eb", 2052 };
        paramMap.add("parameters", parameters);

        // HttpEntity表示http的request和resposne实体
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(paramMap, headers);

        // 发送POST请求并获取返回结果
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);

        System.out.println("result:" + response.getBody());
        return "ok";

    }

    @GetMapping("test")
    public String test() throws Exception {


        return "ok";

    }



}
